package cn.cfoodc.sharding.controller;

import cn.cfoodc.sharding.domain.Encryptor;
import cn.cfoodc.sharding.service.EncryptorService;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Encryptor类的name、pwd、
 */
@RestController
public class DemoEncryptorContorller {
    private EncryptorService encryptorService;
    private SnowflakeKeyGenerateAlgorithm NEXT_ID ;
    @Autowired
    public DemoEncryptorContorller(EncryptorService encryptorService , SnowflakeKeyGenerateAlgorithm snowflakeKeyGenerateAlgorithm ){
        this.encryptorService=encryptorService;
        this.NEXT_ID=snowflakeKeyGenerateAlgorithm;
    }

    //添加一条记录，按照id % 3 落入到对应库.表中，会发起一条sql
    @PostMapping("/inserEncryptor")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE，这里只引入了XA
    public int insert(@ModelAttribute Encryptor encryptor){
        Encryptor e = new Encryptor();
        e.setId((Long) NEXT_ID.generateKey());
        e.setName(encryptor.getName());
        e.setPwd(encryptor.getPwd());
        e.setPublicKey(encryptor.getPublicKey());
        e.setPrivateKey(encryptor.getPrivateKey());
        return encryptorService.insert(e);
    }

    //按照id规则查询指定表，会发起一条sql
    @GetMapping("/selectEncryptorAndArgs1")
    public Encryptor selectByPrimaryKey(Long id){
        return encryptorService.selectByPrimaryKey(id);
    }

    //全库全表查询，会发起多条sql
    @GetMapping("/selectEncryptorAll")
    public List<Exception> selectAll(){
        return encryptorService.selectAll();
    }

    //按照name、publicKey全库全表查询，会发起多条sql
    @GetMapping("/selectEncryptorAndArgs12")
    public List<Exception> selectByAnd(String name ,String publicKey ){
        return encryptorService.selectByAnd(name,publicKey);
    }


    //按照id规则删除指定表,并且匹配name、publicKey，会发起一条sql
    @PostMapping("/deleteEncryptorAll")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE，这里只引入了XA
    public int deleteByPrimaryKey(Long id , String name ,String publicKey ){
        return encryptorService.deleteByPrimaryKey(id,name,publicKey);
    }

    //按照id查询指定库.表 中的记录更新，会发起一条sql
    @PostMapping("/updateEncryptorAll")
    @Transactional(rollbackFor = { Exception.class })
    @ShardingTransactionType(TransactionType.XA)// 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE，这里只引入了XA
    public int updateByPrimaryKey(@ModelAttribute Encryptor encryptor){
        return encryptorService.updateByPrimaryKey(encryptor);
    }
}
